package com.liuyjy.base.common.util;

import com.liuyjy.base.common.constant.CharacterConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author: liuyjy
 * @Description: 登录拦截方法
 * @date: 2022/2/11 15:32
 */
@Slf4j
public class loginUtil {


    private static final String LOCAL_IP = "127.0.0.1";

    private static final String DEFAULT_IP = "0:0:0:0:0:0:0:1";

    /**
     * 英文逗号分隔符
     */
    private static final String COMMA_DELIMITERS = ",";

    public static void test() {
        System.out.println("sdfdsf");
        System.out.println("sdfdsf");
    }


    public static String getHost(HttpServletRequest request) {
        String host = request.getHeader("x-forwarded-host");
        if (StringUtils.isEmpty(host)) {
            host = request.getHeader("host");
        }
        log.info("返回结果ip============" + host);
        return host;
    }


    /**
     * 获取ip
     *
     * @param request 请求提
     * @return String
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }

    /**
     * 获取客户端IP地址
     *
     * @param request
     * @return
     */
    public static String getIpAddr(ServerHttpRequest request) {
        String header1 = "x-forwarded-for";
        String header2 = "Proxy-Client-IP";
        String header3 = "WL-Proxy-Client-IP";
        String ip1 = "unknown";
        String ip2 = "127.0.0.1";
        String ip3 = "0:0:0:0:0:0:0:1";

        HttpHeaders headers = request.getHeaders();
        String ipAddress = String.valueOf(headers.get(header1));
        if (ipAddress == null || ipAddress.length() == 0 || ip1.equalsIgnoreCase(ipAddress)) {
            ipAddress = String.valueOf(headers.get(header2));
        }
        if (ipAddress == null || ipAddress.length() == 0 || ip1.equalsIgnoreCase(ipAddress)) {
            ipAddress = String.valueOf(headers.get(header3));
        }
        if (ipAddress == null || ipAddress.length() == 0 || ip1.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddress().getHostName();
            if (ip2.equals(ipAddress) || ip3.equals(ipAddress)) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ipAddress = inet.getHostAddress();
            }
        }
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        int i = 15;
        if (ipAddress != null && ipAddress.length() > i) {
            String split = ",";
            if (ipAddress.indexOf(split) > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(split));
            }
        }
        return ipAddress;
    }

    /**
     * 获取ip
     *
     * @param request 请求提
     * @return String
     */
    public static String getIpAddr(HttpServletRequest request) {

        String ipAddress = request.getHeader("x-forwarded-for");
        if (EmptyUtil.isEmpty(ipAddress) || CharacterConstant.UNKNOWN.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (EmptyUtil.isEmpty(ipAddress) || CharacterConstant.UNKNOWN.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }

        if (EmptyUtil.isEmpty(ipAddress) || CharacterConstant.UNKNOWN.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_CLIENT_IP");
        }
        if (EmptyUtil.isEmpty(ipAddress) || CharacterConstant.UNKNOWN.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (EmptyUtil.isEmpty(ipAddress) || CharacterConstant.UNKNOWN.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("x-real-ip");
        }

        if (EmptyUtil.isEmpty(ipAddress) || CharacterConstant.UNKNOWN.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (LOCAL_IP.equalsIgnoreCase(ipAddress) || DEFAULT_IP.equalsIgnoreCase(ipAddress)) {
                // 根据网卡取本机配置的IP
                try {
                    ipAddress = InetAddress.getLocalHost().getHostAddress();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            }
        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (EmptyUtil.isNotEmpty(ipAddress)) {
            String[] ips = ipAddress.split(COMMA_DELIMITERS);
            if (ips.length > 1) {
                ipAddress = ips[0];
            }
        }
        return ipAddress;
    }

}
